package abg.gen;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.JdbcTypeMapping;
import com.zaxxer.hikari.HikariDataSource;

import java.math.BigInteger;
import java.util.HashMap;

public class GenCode {
    private static final HikariDataSource dataSource = new HikariDataSource();
    private static final GlobalConfig globalConfig = new GlobalConfig();
    private static final Generator generator = new Generator(dataSource, globalConfig);

    static {
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/vis?serverTimezone=Asia/Shanghai&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useUnicode=true&useSSL=false&useTimezone=false&allowPublicKeyRetrieval=true");
        dataSource.setUsername("root");
        dataSource.setPassword("1234qwer");
    }

    public static void main(String[] args) {
        globalConfig.setSourceDir(System.getProperty("user.dir") + "/z-gen-code/code");


        //设置表前缀,实体类创建时会排除这个前缀命名，比如 a_demo 创建后 为 Demo.class
        globalConfig.setTablePrefix("a_");

        //生成哪些表
        globalConfig.setGenerateTable("a_demo");

        //忽略生成字段,因为这些字段是默认的公共字段
        //如果不需要继承 ZEntity.class ，那就看情况自选
        globalConfig.getStrategyConfig().setIgnoreColumns("id", "ci", "ui", "ct", "ut", "del");
        //数据类型转换
        JdbcTypeMapping.registerMapping(BigInteger.class, String.class);

        //设置每个类实际项目中应该所在的代码包
        globalConfig.getPackageConfig()
                .setControllerPackage("vis.manage.controller.demo")
                .setServicePackage("vis.core.service.demo")
                .setServiceImplPackage("vis.core.service.demo.impl")
                .setMapperPackage("vis.core.mapper")
                .setEntityPackage("vis.core.domain.db");

        //生成配置
        globalConfig.enableController().setOverwriteEnable(true);
        globalConfig.enableEntity().setOverwriteEnable(true).setWithLombok(true);
        globalConfig.enableMapper().setOverwriteEnable(true);
        globalConfig.enableService().setOverwriteEnable(true);
        globalConfig.enableServiceImpl().setOverwriteEnable(true);

        //追加参数
        HashMap<String, Object> appendParam = new HashMap<>();
        globalConfig.getTemplateConfig()
                .setTemplate(new MyTemplate(appendParam))
                .setController("/templates/controller.tpl")
                .setEntity("/templates/entity.tpl")
                .setMapper("/templates/mapper.tpl")
                .setService("/templates/service.tpl")
                .setServiceImpl("/templates/serviceImpl.tpl");

        //可以单独配置某个列
        //        ColumnConfig columnConfig = new ColumnConfig();
        //        columnConfig.setColumnName("tenant_id");
        //        columnConfig.setLarge(true);
        //        columnConfig.setVersion(true);
        //        globalConfig.setColumnConfig("tb_account", columnConfig);

        generator.generate();
    }

}